<template>
  <div class="app-container">
    <eHeader :param-bean="paramBean" :sup_this="sup_this" />
    <eForm ref="form" :sup_this="sup_this" />
    <!--表格渲染-->
    <DataGridTable
      v-loading="loading"
      ref="mainTable"
      :sup_this="sup_this"
      :data="data"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :default-expand-all="expandAll"
      :show-page="false"
      :all-define-columns="['icon','sort','componentUrl','menuType','isAdmin','isVisable']"
      grid-id="menuMngMainGridId"
      min-height="400px"
      height="500px"
      row-key="id"
      @select-all="gridSelectAllChange"
      @selection-change="handleGridSelectionChange">
      <el-table-column prop="menuName" label="名称">
        <template slot="header" slot-scope="scope">
          <el-link @click="toggleTreeAllExpand">名称 <i class="el-icon-more el-icon--right" /> </el-link>
        </template>
      </el-table-column>
      <DataGridColumnSlot slot="icon" prop="icon" label="图标" align="center" width="50px">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.icon" />
        </template>
      </DataGridColumnSlot>
      <DataGridColumnSlot slot="sort" prop="sort" align="center" width="70px" label="排序">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.sort }}</el-tag>
        </template>
      </DataGridColumnSlot>
      <DataGridColumn slot="componentUrl" :show-overflow-tooltip="true" prop="componentUrl" label="组件路径"/>
      <DataGridColumnSlot slot="menuType" prop="menuType" label="菜单类型" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.menu_type" :key="item.id">
            <el-tag v-if="scope.row.menuType == item.dictCode">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </DataGridColumnSlot>
      <DataGridColumnSlot slot="isAdmin" prop="isAdmin" label="系统管理员" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.yesorno" :key="item.id">
            <el-tag v-if="scope.row.isAdmin == item.dictCode">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </DataGridColumnSlot>
      <DataGridColumnSlot slot="isVisable" prop="isVisable" label="是否可见" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.yesorno" :key="item.id">
            <el-tag v-if="scope.row.isVisable == item.dictCode">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </DataGridColumnSlot>
      <el-table-column v-if="checkPermission(['SYS_ADMIN'])" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <edit v-permission="['SYS_ADMIN']" :data="scope.row" :sup_this="sup_this"/>
          <el-popover
            v-permission="['SYS_ADMIN']"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" :disabled="scope.row.id === 1" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </DataGridTable>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import eForm from './module/form'
import initData from '@/mixins/initData'
import { del, loadDataUrl } from '@/api/system/menu'

export default {
  components: { eHeader, edit, eForm },
  mixins: [initData],
  // 设置数据字典
  dictTypes: ['menu_type', 'yesorno'],
  data() {
    return {
      delLoading: false, sup_this: this
    }
  },
  created() {
    this.url = loadDataUrl()
    this.searchToggle = true
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    async beforeInit() {
      this.pageCount = false
      return true
    },
    subDelete(id) {
      this.delLoading = true
      const data = { idList: [id] }
      del(data).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
